/*
 * Copyright (c)  2015~2020, xforceplus
 * All rights reserved.
 * Project:datacare-orm
 * Id: MapperInvokeDataConfig.java   2020-09-29 21-41-43
 * Author: Evan
 */
package cn.datacare.orm.mapper.builder;

import cn.datacare.orm.mapper.MapperInvoke;
import cn.datacare.orm.mapper.impl.*;
import cn.datacare.orm.mapper.inovke.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * Title:
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Copyright: 2015~2020
 * </p>
 * <p>
 * Company/Department: xforceplus
 * </p>
 *
 * @author Evan
 * <b>Creation Time:</b> 2020-09-29 21-41-43
 * @since V1.0
 */
public class MapperInvokeDataConfig {
    /**
     * 处理用户自定义方法的代理
     */
    static final Map<Integer, MapperInvoke> METHOD_DESC_PROXY_MAP = new HashMap<>();
    /**
     * beetlsql内置映射好的方法, 是提供给: AmiInnerProxyMapperInvoke 对象使用的.
     * 或者提供给其他自定义的BaseMapper使用
     */
    static final Map<String, MapperInvoke> INTERNAL_AMI_METHOD = new HashMap<>();

    /**处理 @Sql注解*/
    private static final  MapperInvoke SQL_READY_INVOKE = new SqlReadyExecuteMapperInvoke();

    /**
     * beetlsql BaseMapper的处理构建器
     */
    static final MapperConfigBuilder BASE_MAPPER_BUILDER;

    static {
        // 处理用户自定义方法的代理, 提供给MethodDesc.type使用的服务.
        METHOD_DESC_PROXY_MAP.put(0, new InsertMapperInvoke());
        METHOD_DESC_PROXY_MAP.put(1, new InsertMapperInvoke());
        METHOD_DESC_PROXY_MAP.put(2, new SelectSingleMapperInvoke());
        METHOD_DESC_PROXY_MAP.put(3, new SelectMapperInvoke());
        METHOD_DESC_PROXY_MAP.put(4, new UpdateMapperInvoke());
        METHOD_DESC_PROXY_MAP.put(5, new UpdateBatchMapperInvoke());
        METHOD_DESC_PROXY_MAP.put(6, new PageQueryMapperInvoke());
    }

    static {
        // 添加内置的 INTERNAL_AMI_METHOD
        INTERNAL_AMI_METHOD.put("upsert", new UpsertAnnotationMapperInvokeImpl());
        INTERNAL_AMI_METHOD.put("upsertByTemplate", new UpsertByTemplateAnnotationMapperInvokeImpl());
        INTERNAL_AMI_METHOD.put("insert", new InsertAnnotationMapperInvokeImpl());
        INTERNAL_AMI_METHOD.put("insertReturnKey", new InsertReturnKeyAnnotationMapperInvokeImpl());
        INTERNAL_AMI_METHOD.put("updateById", new UpdateByIdAnnotationMapperInvokeImpl());
        INTERNAL_AMI_METHOD.put("updateTemplateById", new UpdateTemplateByIdAnnotationMapperInvokeImpl());
        INTERNAL_AMI_METHOD.put("deleteById", new DeleteByIdAnnotationMapperInvokeImpl());
        INTERNAL_AMI_METHOD.put("unique", new UniqueAnnotationMapperInvokeImpl());
        INTERNAL_AMI_METHOD.put("single", new SingleAnnotationMapperInvokeImpl());
        INTERNAL_AMI_METHOD.put("lock", new LockAnnotationMapperInvokeImpl());
        INTERNAL_AMI_METHOD.put("all", new AllAnnotationMapperInvokeImpl());
        INTERNAL_AMI_METHOD.put("allCount", new AllCountAnnotationMapperInvokeImpl());
        INTERNAL_AMI_METHOD.put("template", new TemplateAnnotationMapperInvokeImpl());
        INTERNAL_AMI_METHOD.put("templateOne", new TemplateOneAnnotationMapperInvokeImpl());
        INTERNAL_AMI_METHOD.put("templateCount", new TemplateCountAnnotationMapperInvokeImpl());
        INTERNAL_AMI_METHOD.put("updateByIdBatch", new UpdateByIdBatchAnnotationMapperInvokeImpl());
        INTERNAL_AMI_METHOD.put("execute", new ExecuteAnnotationMapperInvokeImpl());
        INTERNAL_AMI_METHOD.put("executeUpdate", new ExecuteUpdateAnnotationMapperInvokeImpl());
        INTERNAL_AMI_METHOD.put("insertBatch", new InsertBatchAnnotationMapperInvokeImpl());
        INTERNAL_AMI_METHOD.put("getSQLManager", new GetSQLManagerAnnotationMapperInvokeImpl());
        INTERNAL_AMI_METHOD.put("insertTemplate", new InsertTemplateAnnotationMapperInvokeImpl());
        INTERNAL_AMI_METHOD.put("templatePage", new TemplatePageAnnotationMapperInvokeImpl());
        INTERNAL_AMI_METHOD.put("createQuery", new QueryAnnotationMapperInvokeImpl());
        INTERNAL_AMI_METHOD.put("createLambdaQuery", new LambdaQueryAnnotationMapperInvokeImpl());
        INTERNAL_AMI_METHOD.put("getTargetEntity", new GetTargetEntityAnnotationMapperInvokeImpl());


        BASE_MAPPER_BUILDER = new MapperConfigBuilder();
    }



    /**
     * 如果用户要扩展, 推荐methodDescType变量使用1000以后的
     *
     * @param methodDescType 类型
     * @param mapperInvoke   映射类处理.
     */
    public static void putMethodDescProxy(int methodDescType, MapperInvoke mapperInvoke) {
        METHOD_DESC_PROXY_MAP.put(methodDescType, mapperInvoke);
    }

    public static MapperInvoke getMethodDescProxy(int methodDescType) {
        return METHOD_DESC_PROXY_MAP.get(methodDescType);
    }

    public static MapperInvoke getSqlReadyProxy() {
        return SQL_READY_INVOKE;
    }
}
